package com.prestamomo.vil.ppa.pmvilpage;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilGoods;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilLeftOver;
import com.prestamomo.vil.ppa.pmvilapi.PmvilRoute;
import com.prestamomo.vil.ppa.pmvilapi.PmvilStill;
import com.prestamomo.vil.ppa.pmvilapi.PmvilStillResult;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilRouteFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import java.util.List;

public class PmvilStillVy extends PmvilAtyRoot {

    private PmvilGoods.LoanOrderRecordsBean pmvilGdsBen;
    private ImageView stillPmvilIvback;
    private ImageView stillPmvilIvicon;
    private TextView stillPmvilTvname;
    private TextView stillPmvilTvdked;
    private TextView stillPmvilTvdkrq;
    private TextView stillPmvilTvyfje;
    private TextView still_pmvil_tv_kbf;
    private TextView still_pmvil_tv_fcf;
    private TextView stillPmvilTvznj;
    private TextView stillPmvilTvdqrq;
    private TextView stillPmvilTvhk;
    private TextView stillPmvilTvlx;
    private LinearLayout stillPmvilLlzq;
    private TextView stillPmvilTvzqyfje;
    private TextView stillPmvilTvzqdarq;
    private TextView still_pmvil_zqtv_kbf;
    private TextView stillPmvilTvjnfy;
    private TextView stillPmvilTvzahk;
    private PmvilStill pmvilStillResultBeans;
    private PmvilLeftOver mPmvilLeftOver;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.still_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        stillPmvilIvback = findViewById(R.id.still_pmvil_iv_back);
        stillPmvilIvicon = findViewById(R.id.still_pmvil_iv_icon);
        stillPmvilTvname = findViewById(R.id.still_pmvil_tv_name);
        stillPmvilTvdked = findViewById(R.id.still_pmvil_tv_dked);
        stillPmvilTvdkrq = findViewById(R.id.still_pmvil_tv_dkrq);
        still_pmvil_tv_fcf = findViewById(R.id.still_pmvil_tv_fcf);
        stillPmvilTvyfje = findViewById(R.id.still_pmvil_tv_yfje);
        still_pmvil_tv_kbf = findViewById(R.id.still_pmvil_tv_kbf);
        stillPmvilTvznj = findViewById(R.id.still_pmvil_tv_znj);
        stillPmvilTvdqrq = findViewById(R.id.still_pmvil_tv_dqrq);
        stillPmvilTvhk = findViewById(R.id.still_pmvil_tv_hk);
        stillPmvilTvlx = findViewById(R.id.still_pmvil_tv_lx);
        stillPmvilLlzq = findViewById(R.id.still_pmvil_ll_zq);
        stillPmvilTvzqyfje = findViewById(R.id.still_pmvil_tv_zqyfje);
        stillPmvilTvzqdarq = findViewById(R.id.still_pmvil_tv_zqdqrq);
        still_pmvil_zqtv_kbf = findViewById(R.id.still_pmvil_zqtv_kbf);
        stillPmvilTvjnfy = findViewById(R.id.still_pmvil_tv_jnfy);
        stillPmvilTvzahk = findViewById(R.id.still_pmvil_tv_zqhk);
    }

    @Override
    protected void clickePmvil() {
        stillPmvilIvback.setOnClickListener(v -> finish());
        stillPmvilTvhk.setOnClickListener(v -> getRoutePmvilInfo(pmvilStillResultBeans.getRepayPlanId(),0));
        stillPmvilTvzahk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getRoutePmvilInfo(pmvilStillResultBeans.getRepayPlanId(),1);
            }
        });
    }

    @Override
    protected void loadPmvilData() {
        pmvilGdsBen = (PmvilGoods.LoanOrderRecordsBean) getIntent().getSerializableExtra("gds_pmvil");
        getStillPmvilInfo(pmvilGdsBen.getApplyId());
        getPmvilKoslw("pmvilStilldkie","pmvilStillKisw");
    }

    private void getStillPmvilInfo(String pmvilRepayId) {
        showPmvilLoading();
        PmvilHttpRequest.getStillPmvilInfo(this, pmvilRepayId, new PmvilCallBack<PmvilStill>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilStill pmvilContent) {
                pmvilStillResultBeans = pmvilContent;
                if(pmvilStillResultBeans.getExtension()){
                    stillPmvilLlzq.setVisibility(View.VISIBLE);
                    getLeftOverPmvilInfo(pmvilStillResultBeans.getRepayPlanId());
                }else{
                    hindPmvilLoading();
                    stillPmvilLlzq.setVisibility(View.GONE);
                }
                Glide.with(pmvilAy).load(pmvilStillResultBeans.getIcon()).into(stillPmvilIvicon);
                stillPmvilTvname.setText(pmvilStillResultBeans.getProductName());
                stillPmvilTvdked.setText("$"+pmvilStillResultBeans.getApplyAmount());
                still_pmvil_tv_fcf.setText("$"+pmvilStillResultBeans.getReviewAmount());
                stillPmvilTvdkrq.setText(pmvilStillResultBeans.getApplyTime());
                stillPmvilTvyfje.setText("$"+pmvilStillResultBeans.getUnpaid());
                still_pmvil_tv_kbf.setText("$"+pmvilStillResultBeans.getServiceAmount());
                stillPmvilTvznj.setText("(其中，滯納金："+pmvilStillResultBeans.getOverdueAmount()+")");
                stillPmvilTvlx.setText("$"+pmvilStillResultBeans.getInterestAmount());
                stillPmvilTvdqrq.setText(pmvilStillResultBeans.getRepayDate());
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilStill pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilStill pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void getLeftOverPmvilInfo(int pmvilRepayPlanid){
        showPmvilLoading();
        PmvilHttpRequest.getLeftOverPmvilInfo(this, pmvilRepayPlanid, new PmvilCallBack<PmvilLeftOver>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilLeftOver pmvilContent) {
                hindPmvilLoading();
                mPmvilLeftOver = pmvilContent;
                stillPmvilTvzqyfje.setText(mPmvilLeftOver.getPmvilextensionAmount());
                stillPmvilTvzqdarq.setText(mPmvilLeftOver.getPmvilextensionTime());
                still_pmvil_zqtv_kbf.setText(mPmvilLeftOver.getPmvilserviceAmount());
                //stillPmvilTvjnfy.setText(pmvilContent.getPmvilextServiceAmount());
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilLeftOver pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilLeftOver pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                    tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void getRoutePmvilInfo(int pmvilStillRepqyId,int pmvilStillType){
        showPmvilLoading();
        PmvilHttpRequest.getRoutePmvilInfo(this, pmvilStillRepqyId, new PmvilCallBack<PmvilRoute>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilRoute pmvilContent) {
                hindPmvilLoading();
                if(pmvilStillType==0){
                    showBankPmvilTypeDgl(stillPmvilTvhk,pmvilStillType,"付款方式",pmvilContent.getPmvilrepayChannels());
                }else{
                    showBankPmvilTypeDgl(stillPmvilTvzahk,pmvilStillType,"付款方式",pmvilContent.getPmvilrepayChannels());
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilRoute pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilRoute pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }
    private void showBankPmvilTypeDgl(TextView stillPmvilTv,int pmvilStillType, String sourcePmvilStr, List<PmvilRoute.RepayChannelsPmvil> pmvilRoute) {
        View routePmvilView = LayoutInflater.from(this).inflate(R.layout.source_pmvil_pop_ly, null, false);
        final PopupWindow routePmvilPop = new PopupWindow(routePmvilView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        routePmvilPop.setTouchable(true);
        routePmvilPop.showAsDropDown(stillPmvilTv);
        ImageView soucrPmvilPopIvback = routePmvilView.findViewById(R.id.source_pmvil_pop_iv_back);
        TextView soucrPmvilPopTvtitle = routePmvilView.findViewById(R.id.source_pmvil_pop_tv_title);
        RecyclerView soucrPmvilPopRecycle = routePmvilView.findViewById(R.id.source_pmvil_pop_recycle);
        soucrPmvilPopTvtitle.setText(sourcePmvilStr);
        soucrPmvilPopIvback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                routePmvilPop.dismiss();
            }
        });
        PmvilRouteFiter pmvilSourceFiter = new PmvilRouteFiter(pmvilAy, pmvilRoute);

        soucrPmvilPopRecycle.setAdapter(pmvilSourceFiter);
        pmvilSourceFiter.setRoutePmvilClick(pmvilRoutes -> {
            if(pmvilStillType==0){
                comitStillPmvilInfo(pmvilStillResultBeans.getRepayPlanId(),pmvilRoutes.getPmvilvalue());
            }else {
                comitLeftOverPmvilInfo(pmvilStillResultBeans.getRepayPlanId(),pmvilRoutes.getPmvilvalue());
            }
            routePmvilPop.dismiss();
        });
    }


    private void comitStillPmvilInfo(int pmvileRepayId,String pmvilRouteType){
        showPmvilLoading();
        PmvilHttpRequest.comitStillPmvilInfo(this, pmvileRepayId, pmvilRouteType, new PmvilCallBack<PmvilStillResult>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilStillResult pmvilContent) {
                hindPmvilLoading();
                if(pmvilContent.getPmvilstatus().equals("S")){
                    if(!pmvilContent.isManual()){
                        if(!TextUtils.isEmpty(pmvilContent.getPmvilvaNumber())){
                            String pmvilvaNumber = pmvilContent.getPmvilvaNumber();
                            if(!pmvilvaNumber.startsWith("https://")){
                                goToPmvilXyAty("支付","https://"+pmvilvaNumber);
                            }else{
                                goToPmvilXyAty("支付",pmvilvaNumber);
                            }
                            finish();
                        }else{
                            PmvilTosMd.showPmvaMsg("付款失敗，請稍後重試");
                        }
                    }else{
                        startActivity(new Intent(pmvilAy,PmvilXianhuk.class)
                                .putExtra("pmhkje",pmvilStillResultBeans.getApplyAmount()+"")
                                .putExtra("type",0)
                                .putExtra("repayid",pmvileRepayId)
                                .putExtra("qudstr",pmvilRouteType));
                        finish();
                    }
                }else {
                    PmvilTosMd.showPmvaMsg(pmvilContent.getPmvilresMsg());
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilStillResult pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilStillResult pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });

    }

    private void comitLeftOverPmvilInfo(int pmvileRepayId,String pmvilRouteType){
        showPmvilLoading();
        PmvilHttpRequest.comitLeftOverPmvilInfo(this, pmvileRepayId, pmvilRouteType, new PmvilCallBack<PmvilStillResult>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilStillResult pmvilContent) {
                hindPmvilLoading();
                if(pmvilContent.getPmvilstatus().equals("S")){
                    if(!pmvilContent.isManual()){
                        if(!TextUtils.isEmpty(pmvilContent.getPmvilvaNumber())){
                            String pmvilvaNumber = pmvilContent.getPmvilvaNumber();
                            if(!pmvilvaNumber.startsWith("https://")){
                                goToPmvilXyAty("支付","https://"+pmvilvaNumber);
                            }else{
                                goToPmvilXyAty("支付",pmvilvaNumber);
                            }
                            finish();
                        }else{
                            PmvilTosMd.showPmvaMsg("付款失敗，請稍後重試");
                        }
                    }else{
                        startActivity(new Intent(pmvilAy,PmvilXianhuk.class)
                                .putExtra("pmhkje",mPmvilLeftOver.getPmvilextensionAmount())
                                .putExtra("type",1)
                                .putExtra("repayid",pmvileRepayId)
                                .putExtra("qudstr",pmvilRouteType));
                        finish();
                    }
                }else {
                    PmvilTosMd.showPmvaMsg(pmvilContent.getPmvilresMsg());
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilStillResult pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilStillResult pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }


    private void getPmvilKoslw(String pmvilStillIls,String pmvilStillKis){
        PmvilHttpRequest.getPmvilKoslw(this, pmvilStillIls, pmvilStillKis, new PmvilCallBack<String>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, String pmvilContent) {

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, String pmvilCtent) {

            }

            @Override
            public void onPmvilOther(String pmvilMhod, String pmvilCnt, String pmvilCode, String pmvilMsg) {

            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {

            }
        });
    }
}
